<?php
/**
 * MVC Ultra - A ultralight weight MVC framework for PHP
 *
 * MVC ultra is very simple MVC framework that is designed to be easy to use and learn.
 * It does not have all of other frameworks but you add what you need when you need it
 * and understand better whats going on.
 *
 *@author Warren Thomas <wiley_warren@yahoo.com>
 *@copyright Copyright (c) 2012.
 *
 ** 20121027/Warren Thomas      - Created MVC Ultra
 *
 */

session_start();
 
// start profile stopwatch
define('START_TIME' , microtime(true));

// physical system path mvc root
define('SERVER_ROOT' , '/home/content/webroot...');

// yoursite.com is your webserver
define('SITE_ROOT' , 'http://my-site.com');

// default view for web site
define('DEFAULT_VIEW','default');

// views folder
define('VIEWS',SERVER_ROOT.'/view');

//helper folder
define('HELPERS',SERVER_ROOT.'/helper');

//Model folder
define('MODELS',SERVER_ROOT.'/model');

// Config file
define('CONFIG_FILE',SERVER_ROOT.'/config.ini');

// default view for exception rendering
function render_error($ex){
    header('HTTP/1.1 500 Internal Server Error',TRUE,500);
    $data['Exception'] = $ex;
    $data['start_time'] = START_TIME;
    $data['view'] = 'include/error';
    include(VIEWS.'/'.DEFAULT_VIEW.'.php');
    exit;
}

//Automatically includes files containing classes that are called
function __autoload( $className ){
    //parse out filename where class should be located
    list( $prefix, $path ) = explode( '_' , $className );
    if ( $prefix =='ultra' ) {
        $path = 'library';
    }
    
    //if( !isset( $path ) ) $prefix = $className;
    if( !isset( $path ) ) $path = 'controller';
    
    //compose file name
    $file = SERVER_ROOT . '/'.$path.'/' . strtolower($className) . '.php';
    
    //fetch file
    if ( file_exists( $file ) )
    {
        //get file
        include_once( $file );        
    } else {
        //file does not exist!
        throw new UnexpectedValueException("File '$file' containing class '$className' not found.");    
    }
    
    // class is not in file
    if( ! class_exists( $className ) ) {
        throw new UnexpectedValueException("Class $className not found in $file");   
    }
}


// Main - start ultra mvc
try{
    $mvc = ultra_mvc::getInstance();
} catch(Exception $ex){
    render_error($ex);
}
